home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_061 / microemacs / random.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  24KB  |  904 lines

  1. /*
  2.  * This file contains the command processing functions for a number of random
  3.  * commands. There is no functional grouping here, for sure.
  4.  */
  5.  
  6. #include        <stdio.h>
  7. #include    "estruct.h"
  8. #include        "edef.h"
  9.  
  10. #if    MEGAMAX & ST520
  11. overlay "random"
  12.  
  13. extern int STncolors;
  14. #endif
  15.  
  16. int     tabsize;                        /* Tab size (0: use real tabs)  */
  17.  
  18. /*
  19.  * Set fill column to n.
  20.  */
  21. setfillcol(f, n)
  22. {
  23.         fillcol = n;
  24.     mlwrite("[Fill column is %d]",n);
  25.         return(TRUE);
  26. }
  27.  
  28. /*
  29.  * Display the current position of the cursor, in origin 1 X-Y coordinates,
  30.  * the character that is under the cursor (in hex), and the fraction of the
  31.  * text that is before the cursor. The displayed column is not the current
  32.  * column, but the column that would be used on an infinite width display.
  33.  * Normally this is bound to "C-X =".
  34.  */
  35. showcpos(f, n)
  36. {
  37.         register LINE   *lp;        /* current line */
  38.         register long   numchars;    /* # of chars in file */
  39.         register int    numlines;    /* # of lines in file */
  40.         register long   predchars;    /* # chars preceding point */
  41.         register int    predlines;    /* # lines preceding point */
  42.         register int    curchar;    /* character under cursor */
  43.         int ratio;
  44.         int col;
  45.     int savepos;            /* temp save for current offset */
  46.     int ecol;            /* column pos/end of current line */
  47.  
  48.     /* starting at the beginning of the buffer */
  49.         lp = lforw(curbp->b_linep);
  50.  
  51.     /* start counting chars and lines */
  52.         numchars = 0;
  53.         numlines = 0;
  54.         while (lp != curbp->b_linep) {
  55.         /* if we are on the current line, record it */
  56.         if (lp == curwp->w_dotp) {
  57.             predlines = numlines;
  58.             predchars = numchars + curwp->w_doto;
  59.             if ((curwp->w_doto) == llength(lp))
  60.                 curchar = '\n';
  61.             else
  62.                 curchar = lgetc(lp, curwp->w_doto);
  63.         }
  64.         /* on to the next line */
  65.         ++numlines;
  66.         numchars += llength(lp) + 1;
  67.         lp = lforw(lp);
  68.         }
  69.  
  70.     /* if at end of file, record it */
  71.     if (curwp->w_dotp == curbp->b_linep) {
  72.         predlines = numlines;
  73.         predchars = numchars;
  74.     }
  75.  
  76.     /* Get real column and end-of-line column. */
  77.     col = getccol(FALSE);
  78.     savepos = curwp->w_doto;
  79.     curwp->w_doto = llength(curwp->w_dotp);
  80.     ecol = getccol(FALSE);
  81.     curwp->w_doto = savepos;
  82.  
  83.         ratio = 0;              /* Ratio before dot. */
  84.         if (numchars != 0)
  85.                 ratio = (100L*predchars) / numchars;
  86.  
  87.     /* summarize and report the info */
  88.     mlwrite("Line %d/%d Col %d/%d Char %D/%D (%d%%) char = 0x%x",
  89.         predlines+1, numlines+1, col, ecol,
  90.         predchars, numchars, ratio, curchar);
  91.         return (TRUE);
  92. }
  93.  
  94. getcline()    /* get the current line number */
  95.  
  96. {
  97.         register LINE   *lp;        /* current line */
  98.         register int    numlines;    /* # of lines before point */
  99.  
  100.     /* starting at the beginning of the buffer */
  101.         lp = lforw(curbp->b_linep);
  102.  
  103.     /* start counting lines */
  104.         numlines = 0;
  105.         while (lp != curbp->b_linep) {
  106.         /* if we are on the current line, record it */
  107.         if (lp == curwp->w_dotp)
  108.             break;
  109.         ++numlines;
  110.         lp = lforw(lp);
  111.         }
  112.  
  113.     /* and return the resulting count */
  114.     return(numlines + 1);
  115. }
  116.  
  117. /*
  118.  * Return current column.  Stop at first non-blank given TRUE argument.
  119.  */
  120. getccol(bflg)
  121. int bflg;
  122. {
  123.         register int c, i, col;
  124.         col = 0;
  125.         for (i=0; i<curwp->w_doto; ++i) {
  126.                 c = lgetc(curwp->w_dotp, i);
  127.                 if (c!=' ' && c!='\t' && bflg)
  128.                         break;
  129.                 if (c == '\t')
  130.                         col |= 0x07;
  131.                 else if (c<0x20 || c==0x7F)
  132.                         ++col;
  133.                 ++col;
  134.         }
  135.         return(col);
  136. }
  137.  
  138. /*
  139.  * Set current column.
  140.  */
  141. setccol(pos)
  142.  
  143. int pos;    /* position to set cursor */
  144.  
  145. {
  146.         register int c;        /* character being scanned */
  147.     register int i;        /* index into current line */
  148.     register int col;    /* current cursor column   */
  149.     register int llen;    /* length of line in bytes */
  150.  
  151.     col = 0;
  152.     llen = llength(curwp->w_dotp);
  153.  
  154.     /* scan the line until we are at or past the target column */
  155.     for (i = 0; i < llen; ++i) {
  156.         /* upon reaching the target, drop out */
  157.         if (col >= pos)
  158.             break;
  159.  
  160.         /* advance one character */
  161.                 c = lgetc(curwp->w_dotp, i);
  162.                 if (c == '\t')
  163.                         col |= 0x07;
  164.                 else if (c<0x20 || c==0x7F)
  165.                         ++col;
  166.                 ++col;
  167.         }
  168.     /* if not long enough... */
  169.     if (col < pos)
  170.         return(FALSE);
  171.  
  172.     /* otherwise...set us at the new position */
  173.     curwp->w_doto = i;
  174.     return(TRUE);
  175. }
  176.  
  177. /*
  178.  * Twiddle the two characters on either side of dot. If dot is at the end of
  179.  * the line twiddle the two characters before it. Return with an error if dot
  180.  * is at the beginning of line; it seems to be a bit pointless to make this
  181.  * work. This fixes up a very common typo with a single stroke. Normally bound
  182.  * to "C-T". This always works within a line, so "WFEDIT" is good enough.
  183.  */
  184. twiddle(f, n)
  185. {
  186.         register LINE   *dotp;
  187.         register int    doto;
  188.         register int    cl;
  189.         register int    cr;
  190.  
  191.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  192.         return(rdonly());    /* we are in read only mode    */
  193.         dotp = curwp->w_dotp;
  194.         doto = curwp->w_doto;
  195.         if (doto==llength(dotp) && --doto<0)
  196.                 return (FALSE);
  197.         cr = lgetc(dotp, doto);
  198.         if (--doto < 0)
  199.                 return (FALSE);
  200.         cl = lgetc(dotp, doto);
  201.         lputc(dotp, doto+0, cr);
  202.         lputc(dotp, doto+1, cl);
  203.         lchange(WFEDIT);
  204.         return (TRUE);
  205. }
  206.  
  207. /*
  208.  * Quote the next character, and insert it into the buffer. All the characters
  209.  * are taken literally, with the exception of the newline, which always has
  210.  * its line splitting meaning. The character is always read, even if it is
  211.  * inserted 0 times, for regularity. Bound to "C-Q"
  212.  */
  213. quote(f, n)
  214. {
  215.         register int    s;
  216.         register int    c;
  217.  
  218.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  219.         return(rdonly());    /* we are in read only mode    */
  220.         c = tgetc();
  221.         if (n < 0)
  222.                 return (FALSE);
  223.         if (n == 0)
  224.                 return (TRUE);
  225.         if (c == '\n') {
  226.                 do {
  227.                         s = lnewline();
  228.                 } while (s==TRUE && --n);
  229.                 return (s);
  230.         }
  231.         return (linsert(n, c));
  232. }
  233.  
  234. /*
  235.  * Set tab size if given non-default argument (n <> 1).  Otherwise, insert a
  236.  * tab into file.  If given argument, n, of zero, change to true tabs.
  237.  * If n > 1, simulate tab stop every n-characters using spaces. This has to be
  238.  * done in this slightly funny way because the tab (in ASCII) has been turned
  239.  * into "C-I" (in 10 bit code) already. Bound to "C-I".
  240.  */
  241. tab(f, n)
  242. {
  243.         if (n < 0)
  244.                 return (FALSE);
  245.         if (n == 0 || n > 1) {
  246.                 tabsize = n;
  247.                 return(TRUE);
  248.         }
  249.         if (! tabsize)
  250.                 return(linsert(1, '\t'));
  251.         return(linsert(tabsize - (getccol(FALSE) % tabsize), ' '));
  252. }
  253.  
  254. /*
  255.  * Open up some blank space. The basic plan is to insert a bunch of newlines,
  256.  * and then back up over them. Everything is done by the subcommand
  257.  * procerssors. They even handle the looping. Normally this is bound to "C-O".
  258.  */
  259. openline(f, n)
  260. {
  261.         register int    i;
  262.         register int    s;
  263.  
  264.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  265.         return(rdonly());    /* we are in read only mode    */
  266.         if (n < 0)
  267.                 return (FALSE);
  268.         if (n == 0)
  269.                 return (TRUE);
  270.         i = n;                                  /* Insert newlines.     */
  271.         do {
  272.                 s = lnewline();
  273.         } while (s==TRUE && --i);
  274.         if (s == TRUE)                          /* Then back up overtop */
  275.                 s = backchar(f, n);             /* of them all.         */
  276.         return (s);
  277. }
  278.  
  279. /*
  280.  * Insert a newline. Bound to "C-M". If we are in CMODE, do automatic
  281.  * indentation as specified.
  282.  */
  283. newline(f, n)
  284. {
  285.     register int    s;
  286.  
  287.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  288.         return(rdonly());    /* we are in read only mode    */
  289.     if (n < 0)
  290.         return (FALSE);
  291.  
  292.     /* if we are in C mode and this is a default <NL> */
  293.     if (n == 1 && (curbp->b_mode & MDCMOD) &&
  294.         curwp->w_dotp != curbp->b_linep)
  295.         return(cinsert());
  296.  
  297.         /*
  298.          * If a newline was typed, fill column is defined, the argument is non-
  299.          * negative, wrap mode is enabled, and we are now past fill column,
  300.      * and we are not read-only, perform word wrap.
  301.          */
  302.         if ((curwp->w_bufp->b_mode & MDWRAP) && fillcol > 0 &&
  303.         getccol(FALSE) > fillcol &&
  304.         (curwp->w_bufp->b_mode & MDVIEW) == FALSE)
  305.         execute(META|SPEC|'W', FALSE, 1);
  306.  
  307.     /* insert some lines */
  308.     while (n--) {
  309.         if ((s=lnewline()) != TRUE)
  310.             return (s);
  311.     }
  312.     return (TRUE);
  313. }
  314.  
  315. cinsert()    /* insert a newline and indentation for C */
  316.  
  317. {
  318.     register char *cptr;    /* string pointer into text to copy */
  319.     register int tptr;    /* index to scan into line */
  320.     register int bracef;    /* was there a brace at the end of line? */
  321.     register int i;
  322.     char ichar[NSTRING];    /* buffer to hold indent of last line */
  323.  
  324.     /* grab a pointer to text to copy indentation from */
  325.     cptr = &curwp->w_dotp->l_text[0];
  326.  
  327.     /* check for a brace */
  328.     tptr = curwp->w_doto - 1;
  329.     bracef = (cptr[tptr] == '{');
  330.  
  331.     /* save the indent of the previous line */
  332.     i = 0;
  333.     while ((i < tptr) && (cptr[i] == ' ' || cptr[i] == '\t')
  334.         && (i < NSTRING - 1)) {
  335.         ichar[i] = cptr[i];
  336.         ++i;
  337.     }
  338.     ichar[i] = 0;        /* terminate it */
  339.  
  340.     /* put in the newline */
  341.     if (lnewline() == FALSE)
  342.         return(FALSE);
  343.  
  344.     /* and the saved indentation */
  345.     i = 0;
  346.     while (ichar[i])
  347.         linsert(1, ichar[i++]);
  348.  
  349.     /* and one more tab for a brace */
  350.     if (bracef)
  351.         tab(FALSE, 1);
  352.  
  353.     return(TRUE);
  354. }
  355.  
  356. insbrace(n, c)    /* insert a brace into the text here...we are in CMODE */
  357.  
  358. int n;    /* repeat count */
  359. int c;    /* brace to insert (always { for now) */
  360.  
  361. {
  362.     register int ch;    /* last character before input */
  363.     register int i;
  364.     register int target;    /* column brace should go after */
  365.  
  366.     /* if we are at the beginning of the line, no go */
  367.     if (curwp->w_doto == 0)
  368.         return(linsert(n,c));
  369.         
  370.     /* scan to see if all space before this is white space */
  371.     for (i = curwp->w_doto - 1; i >= 0; --i) {
  372.         ch = lgetc(curwp->w_dotp, i);
  373.         if (ch != ' ' && ch != '\t')
  374.             return(linsert(n, c));
  375.     }
  376.  
  377.     /* delete back first */
  378.     target = getccol(FALSE);    /* calc where we will delete to */
  379.     target -= 1;
  380.     target -= target % (tabsize == 0 ? 8 : tabsize);
  381.     while (getccol(FALSE) > target)
  382.         backdel(FALSE, 1);
  383.  
  384.     /* and insert the required brace(s) */
  385.     return(linsert(n, c));
  386. }
  387.  
  388. inspound()    /* insert a # into the text here...we are in CMODE */
  389.  
  390. {
  391.     register int ch;    /* last character before input */
  392.     register int i;
  393.  
  394.     /* if we are at the beginning of the line, no go */
  395.     if (curwp->w_doto == 0)
  396.         return(linsert(1,'#'));
  397.         
  398.     /* scan to see if all space before this is white space */
  399.     for (i = curwp->w_doto - 1; i >= 0; --i) {
  400.         ch = lgetc(curwp->w_dotp, i);
  401.         if (ch != ' ' && ch != '\t')
  402.             return(linsert(1, '#'));
  403.     }
  404.  
  405.     /* delete back first */
  406.     while (getccol(FALSE) >= 1)
  407.         backdel(FALSE, 1);
  408.  
  409.     /* and insert the required pound */
  410.     return(linsert(1, '#'));
  411. }
  412.  
  413. /*
  414.  * Delete blank lines around dot. What this command does depends if dot is
  415.  * sitting on a blank line. If dot is sitting on a blank line, this command
  416.  * deletes all the blank lines above and below the current line. If it is
  417.  * sitting on a non blank line then it deletes all of the blank lines after
  418.  * the line. Normally this command is bound to "C-X C-O". Any argument is
  419.  * ignored.
  420.  */
  421. deblank(f, n)
  422. {
  423.         register LINE   *lp1;
  424.         register LINE   *lp2;
  425.         long nld;
  426.  
  427.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  428.         return(rdonly());    /* we are in read only mode    */
  429.         lp1 = curwp->w_dotp;
  430.         while (llength(lp1)==0 && (lp2=lback(lp1))!=curbp->b_linep)
  431.                 lp1 = lp2;
  432.         lp2 = lp1;
  433.         nld = 0;
  434.         while ((lp2=lforw(lp2))!=curbp->b_linep && llength(lp2)==0)
  435.                 ++nld;
  436.         if (nld == 0)
  437.                 return (TRUE);
  438.         curwp->w_dotp = lforw(lp1);
  439.         curwp->w_doto = 0;
  440.         return (ldelete(nld, FALSE));
  441. }
  442.  
  443. /*
  444.  * Insert a newline, then enough tabs and spaces to duplicate the indentation
  445.  * of the previous line. Assumes tabs are every eight characters. Quite simple.
  446.  * Figure out the indentation of the current line. Insert a newline by calling
  447.  * the standard routine. Insert the indentation by inserting the right number
  448.  * of tabs and spaces. Return TRUE if all ok. Return FALSE if one of the
  449.  * subcomands failed. Normally bound to "C-J".
  450.  */
  451. indent(f, n)
  452. {
  453.         register int    nicol;
  454.         register int    c;
  455.         register int    i;
  456.  
  457.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  458.         return(rdonly());    /* we are in read only mode    */
  459.         if (n < 0)
  460.                 return (FALSE);
  461.         while (n--) {
  462.                 nicol = 0;
  463.                 for (i=0; i<llength(curwp->w_dotp); ++i) {
  464.                         c = lgetc(curwp->w_dotp, i);
  465.                         if (c!=' ' && c!='\t')
  466.                                 break;
  467.                         if (c == '\t')
  468.                                 nicol |= 0x07;
  469.                         ++nicol;
  470.                 }
  471.                 if (lnewline() == FALSE
  472.                 || ((i=nicol/8)!=0 && linsert(i, '\t')==FALSE)
  473.                 || ((i=nicol%8)!=0 && linsert(i,  ' ')==FALSE))
  474.                         return (FALSE);
  475.         }
  476.         return (TRUE);
  477. }
  478.  
  479. /*
  480.  * Delete forward. This is real easy, because the basic delete routine does
  481.  * all of the work. Watches for negative arguments, and does the right thing.
  482.  * If any argument is present, it kills rather than deletes, to prevent loss
  483.  * of text if typed with a big argument. Normally bound to "C-D".
  484.  */
  485. forwdel(f, n)
  486. {
  487.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  488.         return(rdonly());    /* we are in read only mode    */
  489.         if (n < 0)
  490.                 return (backdel(f, -n));
  491.         if (f != FALSE) {                       /* Really a kill.       */
  492.                 if ((lastflag&CFKILL) == 0)
  493.                         kdelete();
  494.                 thisflag |= CFKILL;
  495.         }
  496.         return (ldelete((long)n, f));
  497. }
  498.  
  499. /*
  500.  * Delete backwards. This is quite easy too, because it's all done with other
  501.  * functions. Just move the cursor back, and delete forwards. Like delete
  502.  * forward, this actually does a kill if presented with an argument. Bound to
  503.  * both "RUBOUT" and "C-H".
  504.  */
  505. backdel(f, n)
  506. {
  507.         register int    s;
  508.  
  509.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  510.         return(rdonly());    /* we are in read only mode    */
  511.         if (n < 0)
  512.                 return (forwdel(f, -n));
  513.         if (f != FALSE) {                       /* Really a kill.       */
  514.                 if ((lastflag&CFKILL) == 0)
  515.                         kdelete();
  516.                 thisflag |= CFKILL;
  517.         }
  518.         if ((s=backchar(f, n)) == TRUE)
  519.                 s = ldelete((long)n, f);
  520.         return (s);
  521. }
  522.  
  523. /*
  524.  * Kill text. If called without an argument, it kills from dot to the end of
  525.  * the line, unless it is at the end of the line, when it kills the newline.
  526.  * If called with an argument of 0, it kills from the start of the line to dot.
  527.  * If called with a positive argument, it kills from dot forward over that
  528.  * number of newlines. If called with a negative argument it kills backwards
  529.  * that number of newlines. Normally bound to "C-K".
  530.  */
  531. killtext(f, n)
  532. {
  533.         register LINE   *nextp;
  534.         long chunk;
  535.  
  536.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  537.         return(rdonly());    /* we are in read only mode    */
  538.         if ((lastflag&CFKILL) == 0)             /* Clear kill buffer if */
  539.                 kdelete();                      /* last wasn't a kill.  */
  540.         thisflag |= CFKILL;
  541.         if (f == FALSE) {
  542.                 chunk = llength(curwp->w_dotp)-curwp->w_doto;
  543.                 if (chunk == 0)
  544.                         chunk = 1;
  545.         } else if (n == 0) {
  546.                 chunk = curwp->w_doto;
  547.                 curwp->w_doto = 0;
  548.         } else if (n > 0) {
  549.                 chunk = llength(curwp->w_dotp)-curwp->w_doto+1;
  550.                 nextp = lforw(curwp->w_dotp);
  551.                 while (--n) {
  552.                         if (nextp == curbp->b_linep)
  553.                                 return (FALSE);
  554.                         chunk += llength(nextp)+1;
  555.                         nextp = lforw(nextp);
  556.                 }
  557.         } else {
  558.                 mlwrite("neg kill");
  559.                 return (FALSE);
  560.         }
  561.         return(ldelete(chunk, TRUE));
  562. }
  563.  
  564. setmode(f, n)    /* prompt and set an editor mode */
  565.  
  566. int f, n;    /* default and argument */
  567.  
  568. {
  569.     adjustmode(TRUE, FALSE);
  570. }
  571.  
  572. delmode(f, n)    /* prompt and delete an editor mode */
  573.  
  574. int f, n;    /* default and argument */
  575.  
  576. {
  577.     adjustmode(FALSE, FALSE);
  578. }
  579.  
  580. setgmode(f, n)    /* prompt and set a global editor mode */
  581.  
  582. int f, n;    /* default and argument */
  583.  
  584. {
  585.     adjustmode(TRUE, TRUE);
  586. }
  587.  
  588. delgmode(f, n)    /* prompt and delete a global editor mode */
  589.  
  590. int f, n;    /* default and argument */
  591.  
  592. {
  593.     adjustmode(FALSE, TRUE);
  594. }
  595.  
  596. adjustmode(kind, global)    /* change the editor mode status */
  597.  
  598. int kind;    /* true = set,        false = delete */
  599. int global;    /* true = global flag,    false = current buffer flag */
  600. {
  601.     register char *scan;        /* scanning pointer to convert prompt */
  602.     register int i;            /* loop index */
  603. #if    COLOR
  604.     register int uflag;        /* was modename uppercase?    */
  605. #endif
  606.     char prompt[50];    /* string to prompt user with */
  607.     char cbuf[NPAT];        /* buffer to recieve mode name into */
  608.  
  609.     /* build the proper prompt string */
  610.     if (global)
  611.         strcpy(prompt,"Global mode to ");
  612.     else
  613.         strcpy(prompt,"Mode to ");
  614.  
  615.     if (kind == TRUE)
  616.         strcat(prompt, "add: ");
  617.     else
  618.         strcat(prompt, "delete: ");
  619.  
  620.     /* prompt the user and get an answer */
  621.  
  622.     mlreply(prompt, cbuf, NPAT - 1);
  623.  
  624.     /* make it uppercase */
  625.  
  626.     scan = cbuf;
  627. #if    COLOR
  628.     uflag = (*scan >= 'A' && *scan <= 'Z');
  629. #endif
  630.     while (*scan != 0) {
  631.         if (*scan >= 'a' && *scan <= 'z')
  632.             *scan = *scan - 32;
  633.         scan++;
  634.     }
  635.  
  636.     /* test it first against the colors we know */
  637.     for (i=0; i<NCOLORS; i++) {
  638.         if (strcmp(cbuf, cname[i]) == 0) {
  639.             /* finding the match, we set the color */
  640. #if    COLOR
  641.             if (uflag)
  642.                 if (global)
  643.                     gfcolor = i;
  644.                 else
  645.                     curwp->w_fcolor = i;
  646.             else
  647.                 if (global)
  648.                     gbcolor = i;
  649.                 else
  650.                     curwp->w_bcolor = i;
  651.  
  652.             curwp->w_flag |= WFCOLR;
  653. #endif
  654.             mlerase();
  655.             return(TRUE);
  656.         }
  657.     }
  658.  
  659.     /* test it against the modes we know */
  660.  
  661.     for (i=0; i < NUMMODES; i++) {
  662.         if (strcmp(cbuf, modename[i]) == 0) {
  663.             /* finding a match, we process it */
  664.             if (kind == TRUE)
  665.                 if (global)
  666.                     gmode |= (1 << i);
  667.                 else
  668.                     curwp->w_bufp->b_mode |= (1 << i);
  669.             else
  670.                 if (global)
  671.                     gmode &= ~(1 << i);
  672.                 else
  673.                     curwp->w_bufp->b_mode &= ~(1 << i);
  674.             /* display new mode line */
  675.             if (global == 0)
  676.                 upmode();
  677.             mlerase();    /* erase the junk */
  678.             return(TRUE);
  679.         }
  680.     }
  681.  
  682.     mlwrite("No such mode!");
  683.     return(FALSE);
  684. }
  685.  
  686. /*    This function simply clears the message line,
  687.         mainly for macro usage            */
  688.  
  689. clrmes(f, n)
  690.  
  691. int f, n;    /* arguments ignored */
  692.  
  693. {
  694.     mlwrite("");
  695.     return(TRUE);
  696. }
  697.  
  698. /*    This function writes a string on the message line
  699.         mainly for macro usage            */
  700.  
  701. writemsg(f, n)
  702.  
  703. int f, n;    /* arguments ignored */
  704.  
  705. {
  706.     register char *sp;    /* pointer into buf to expand %s */
  707.     register char *np;    /* ptr into nbuf */
  708.     register int status;
  709.     char buf[NPAT];        /* buffer to recieve message into */
  710.     char nbuf[NPAT*2];    /* buffer to expand string into */
  711.  
  712.     if ((status = mlreply("Message to write: ", buf, NPAT - 1)) != TRUE)
  713.         return(status);
  714.  
  715.     /* expand all '%' to "%%" so mlwrite won't expect arguments */
  716.     sp = buf;
  717.     np = nbuf;
  718.     while (*sp) {
  719.         *np++ = *sp;
  720.         if (*sp++ == '%')
  721.             *np++ = '%';
  722.     }
  723.     *np = '\0';
  724.     mlwrite(nbuf);
  725.     return(TRUE);
  726. }
  727.  
  728. #if    CFENCE
  729. /*    the cursor is moved to a matching fence    */
  730.  
  731. getfence(f, n)
  732.  
  733. int f, n;    /* not used */
  734.  
  735. {
  736.     register LINE *oldlp;    /* original line pointer */
  737.     register int oldoff;    /* and offset */
  738.     register int sdir;    /* direction of search (1/-1) */
  739.     register int count;    /* current fence level count */
  740.     register char ch;    /* fence type to match against */
  741.     register char ofence;    /* open fence */
  742.     register char c;    /* current character in scan */
  743.  
  744.     /* save the original cursor position */
  745.     oldlp = curwp->w_dotp;
  746.     oldoff = curwp->w_doto;
  747.  
  748.     /* get the current character */
  749.     ch = lgetc(oldlp, oldoff);
  750.  
  751.     /* setup proper matching fence */
  752.     switch (ch) {
  753.         case '(': ofence = ')'; sdir = FORWARD; break;
  754.         case '{': ofence = '}'; sdir = FORWARD; break;
  755.         case '[': ofence = ']'; sdir = FORWARD; break;
  756.         case ')': ofence = '('; sdir = REVERSE; break;
  757.         case '}': ofence = '{'; sdir = REVERSE; break;
  758.         case ']': ofence = '['; sdir = REVERSE; break;
  759.         default: TTbeep(); return(FALSE);
  760.     }
  761.  
  762.     /* set up for scan */
  763.     count = 1;
  764.     if (sdir == REVERSE)
  765.         backchar(FALSE, 1);
  766.     else
  767.         forwchar(FALSE, 1);
  768.  
  769.     /* scan until we find it, or reach the end of file */
  770.     while (count > 0) {
  771.         c = lgetc(curwp->w_dotp, curwp->w_doto);
  772.         if (c == ch)
  773.             ++count;
  774.         if (c == ofence)
  775.             --count;
  776.         if (sdir == FORWARD)
  777.             forwchar(FALSE, 1);
  778.         else
  779.             backchar(FALSE, 1);
  780.         if (boundry(curwp->w_dotp, curwp->w_doto, sdir))
  781.             break;
  782.     }
  783.  
  784.     /* if count is zero, we have a match, move the sucker */
  785.     if (count == 0) {
  786.         if (sdir == FORWARD)
  787.             backchar(FALSE, 1);
  788.         else
  789.             forwchar(FALSE, 1);
  790.         curwp->w_flag |= WFMOVE;
  791.         return(TRUE);
  792.     }
  793.  
  794.     /* restore the current position */
  795.     curwp->w_dotp = oldlp;
  796.     curwp->w_doto = oldoff;
  797.     TTbeep();
  798.     return(FALSE);
  799. }
  800. #endif
  801.  
  802. /*    Close fences are matched against their partners, and if
  803.     on screen the cursor briefly lights there        */
  804.  
  805. fmatch(ch)
  806.  
  807. char ch;    /* fence type to match against */
  808.  
  809. {
  810.     register LINE *oldlp;    /* original line pointer */
  811.     register int oldoff;    /* and offset */
  812.     register LINE *toplp;    /* top line in current window */
  813.     register int count;    /* current fence level count */
  814.     register char opench;    /* open fence */
  815.     register char c;    /* current character in scan */
  816.     register int i;
  817.  
  818.     /* first get the display update out there */
  819.     update(FALSE);
  820.  
  821.     /* save the original cursor position */
  822.     oldlp = curwp->w_dotp;
  823.     oldoff = curwp->w_doto;
  824.  
  825.     /* setup proper open fence for passed close fence */
  826.     if (ch == ')')
  827.         opench = '(';
  828.     else if (ch == '}')
  829.         opench = '{';
  830.     else
  831.         opench = '[';
  832.  
  833.     /* find the top line and set up for scan */
  834.     toplp = curwp->w_linep->l_bp;
  835.     count = 1;
  836.     backchar(FALSE, 2);
  837.  
  838.     /* scan back until we find it, or reach past the top of the window */
  839.     while (count > 0 && curwp->w_dotp != toplp) {
  840.         c = lgetc(curwp->w_dotp, curwp->w_doto);
  841.         if (c == ch)
  842.             ++count;
  843.         if (c == opench)
  844.             --count;
  845.         backchar(FALSE, 1);
  846.         if (curwp->w_dotp == curwp->w_bufp->b_linep->l_fp &&
  847.             curwp->w_doto == 0)
  848.             break;
  849.     }
  850.  
  851.     /* if count is zero, we have a match, display the sucker */
  852.     /* there is a real machine dependant timing problem here we have
  853.        yet to solve......... */
  854.     if (count == 0) {
  855.         forwchar(FALSE, 1);
  856.         for (i = 0; i < term.t_pause; i++)
  857.             update(FALSE);
  858.     }
  859.  
  860.     /* restore the current position */
  861.     curwp->w_dotp = oldlp;
  862.     curwp->w_doto = oldoff;
  863.     return(TRUE);
  864. }
  865.  
  866. istring(f, n)    /* ask for and insert a string into the current
  867.            buffer at the current point */
  868.  
  869. int f, n;    /* ignored arguments */
  870.  
  871. {
  872.     register char *tp;    /* pointer into string to add */
  873.     register int status;    /* status return code */
  874.     char tstring[NPAT+1];    /* string to add */
  875.  
  876.     /* ask for string to insert */
  877.     status = mlreplyt("String to insert<META>: ", tstring, NPAT, metac);
  878.     if (status != TRUE)
  879.         return(status);
  880.  
  881.     if (f == FALSE)
  882.         n = 1;
  883.  
  884.     if (n < 0)
  885.         n = - n;
  886.  
  887.     /* insert it */
  888.     while (n--) {
  889.         tp = &tstring[0];
  890.         while (*tp) {
  891.             if (*tp == 0x0a)
  892.                 status = lnewline();
  893.             else
  894.                 status = linsert(1, *tp);
  895.             ++tp;
  896.             if (status != TRUE)
  897.                 return(status);
  898.         }
  899.     }
  900.         
  901.     return(TRUE);
  902. }
  903.  
  904.